let comments = {}

comments.install = function (Vue, options) {

  //测试用

  Vue.prototype.GetQueryString=function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)return unescape(r[2]);
    return null;
  }


  Vue.prototype.setCache = function (key, loca_data) {
    if (!window.localStorage) {
      $.alert('浏览器不支持localstorage');
    } else {
      var storage = window.localStorage;
      storage.setItem(key, JSON.stringify(loca_data));
    }
  }

  //获取缓存
  Vue.prototype.getCache = function (key) {
    if (!window.localStorage) {
      $.alert('浏览器不支持localstorage');
    } else {
      var storage = window.localStorage;
      return JSON.parse(storage.getItem(key));
    }
  }

  //删除缓存
  Vue.prototype.deleteCache = function(key) {
    if (!window.localStorage) {
      $.alert('浏览器不支持localstorage');
    } else {
      var storage = window.localStorage;
      storage.removeItem(key);
    }
  }

  //删除所有缓存
  Vue.prototype.deleteAllCache = function() {
    if (!window.localStorage) {
      $.alert('浏览器不支持localstorage');
    } else {
      var storage = window.localStorage;
      storage.clear();
    }
  }

  //获取制定链接的某个值
  Vue.prototype.GetUrlString = function(url, name) {

    if (url.indexOf('?') != -1) {
      url = url.split('?')[1];

    }
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = url.match(reg);
    if (r != null)return unescape(r[2]);
    return null;
  }

  Vue.prototype.$combineParams = function(params) {
    var newParams = this.$comParams();
    for (var n in params) {
      // eval('newParams.' + n + '=' + params[n]);
      newParams[n] = params[n];
    }
    return newParams;
  }

  Vue.prototype.isEmpty = function (aim) {
    if(aim == undefined || aim == null || aim === '' || aim === ' '){
      return true;
    }else {
      return false;
    }
  }

  Vue.prototype.xcx_openid = '';
  Vue.prototype.sid = '';
  Vue.prototype.aid = ''
  Vue.prototype.wx_openid = ''

  //检查登录状态
  //检查登录状态，未登录跳转到登录页
  Vue.prototype.checkLogin = function() {
    var member_info = this.getCache('login_key')
    if (member_info == '' || member_info == undefined) {
      //如果没有缓存 即没有登录 则跳转到登录页
      //。。。。。。。
      console.log('没有登录时进行的操作')

    }else {

      //用户个人信息
      Vue.prototype.xcx_openid = member_info.xcx_openid;
      Vue.prototype.sid = member_info.sid;
      Vue.prototype.aid = member_info.aid;
      Vue.prototype.wx_openid = member_info.wx_openid;
      Vue.prototype.mid = member_info.mid;
    }
  }


  Vue.prototype.$comParams = function () {
    return {xcx_openid: this.xcx_openid, wx_openid: this.wx_openid, sid: this.sid}
  }

  Vue.prototype.getKey = function(type) {
    var xcx_openid = this.xcx_openid || '1';
    var wx_openid = this.wx_openid || '1';
    return type + this.xcx_openid + '_' + this.wx_openid + '_' + this.sid+'_'+this.mid;
  },


  //取消登录
  Vue.prototype.logout = function(type) {
    var newtype = type || 0;
    var member_info = this.getCache('login_key');
    this.deleteAllCache();
    //取消登录后，进行的操作

    //暂时跳转到商城
    this.$router.push({path:'/goods'})

  }

  Vue.prototype.checkBack = function() {
    if (window.history && window.history.pushState) {
      $(window).on('popstate', function () {
        window.history.pushState('forward', null, '#');
        window.history.forward(1);
        // mui.alert("不可回退");
      });
    }
  }

  //微信支付
  Vue.prototype.wxPay = function(params, type) {
    function onBridgeReady() {
      if (typeof WeixinJSBridge === 'undefined') {
        malert('请在微信在打开页面！');
        return false;
      }
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
          "appId": params.appId,     //公众号名称，由商户传入
          "timeStamp": params.timeStamp,         //时间戳，自1970年以来的秒数
          "nonceStr": params.nonceStr, //随机串
          "package": params.package,
          "signType": params.signType,         //微信签名方式：
          "paySign": params.paySign //微信签名
        },
        function (res) {
          switch (res.err_msg) {
            case 'get_brand_wcpay_request:cancel':
              malert('', '用户取消支付！', function () {
                if (parseInt(type) == 1) {
                  /*locationReplace('index.html');*/
                  this.$router.push({path:'/goods'})
                } else {
                  /*locationReplace('water-ticket.html');*/
                  this.$router.push({path:'/qds/bookingWater'});
                }
              });
              break;
            case 'get_brand_wcpay_request:fail':
              this.$alert('支付失败', '提示', {
                type: 'warning'
              }).then(({ result }) => {
                if (result) {
                  this.$router.push({path:'/qds/fail',query:{toType:1}});
                }
              });
              /*malert(res.err_desc, '支付失败', function () {
                locationReplace('fail.html?type=1');
              });*/
              break;
            case 'get_brand_wcpay_request:ok':
              this.$router.push({path:'/qds/success',query:{toType:1}});
              /*locationReplace('success.html?type=' + type);*/
              break;
            default:
              alert(JSON.stringify(res));
              break;
          }
        });
    }

    if (typeof WeixinJSBridge == "undefined") {
      if (document.addEventListener) {
        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
      } else if (document.attachEvent) {
        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
      }
    } else {
      onBridgeReady();
    }
  }

}
export default comments


